package com.lagou.edu.controller;


import com.alibaba.fastjson.JSON;
import com.lagou.edu.form.LoginForm;
import com.lagou.edu.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RequestMapping("/login")
@Controller
public class LoginController {

    @GetMapping
    public String login(HttpServletRequest request, HttpServletResponse response){
        return "/login";
    }
    @PostMapping
    @ResponseBody
    public Result doLogin(@RequestBody LoginForm form, HttpServletRequest request){
        //验证用户名和密码是否正确
        if(form.getUsername().equalsIgnoreCase("admin")&&form.getPassword().equalsIgnoreCase("admin")){
              //执行登录动作
            HttpSession session = request.getSession();
            session.setAttribute("login", JSON.toJSONString(form));
            return Result.success(null);

        }else {
            return Result.fail("用户名或者密码不正确");
        }
    }
}
